package cn.kgc.vue.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.util.ObjectUtil;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.ScorePageDTO;
import cn.kgc.vue.entity.Score;
import cn.kgc.vue.mapper.ScoreMapper;
import cn.kgc.vue.service.ScoreService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;


@Service
@Transactional
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements ScoreService {

    @Autowired
    private ScoreMapper scoreMapper;

    @Override
    public ResponseResult getMonthScore() {
        ResponseResult responseResult = new ResponseResult();
        List<Score> scores = scoreMapper.getMonthScore();
        responseResult.success(ResponseStatus.SUCCESS, scores);
        return responseResult;
    }

    @Override
    public ResponseResult getPage(ScorePageDTO scorePageDTO) {
        // 分页查询
        ResponseResult responseResult = new ResponseResult();
        Page<Score> scorePage = new Page<>(scorePageDTO.getCurrentPage(), scorePageDTO.getLimit());
        LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();
        lambda.eq(StringUtils.isNotBlank(scorePageDTO.getClazz()), Score::getClazz, scorePageDTO.getClazz())
                .eq(ObjectUtil.isNotNull(scorePageDTO.getExamTime()), Score::getExamTime, scorePageDTO.getExamTime())
                .orderByDesc(Score::getExamTime);

        HashMap<String, Object> map = new HashMap<>();
        scoreMapper.selectPage(scorePage, lambda);
        map.put("total", scorePage.getTotal());
        map.put("currentPageData", scorePage.getRecords());

        responseResult.success(ResponseStatus.SUCCESS, map);
        return responseResult;
    }

    @Override
    public ResponseResult batchAdd(MultipartFile file) throws Exception {
        ResponseResult responseResult = new ResponseResult();
        if (file.isEmpty()){
            responseResult.setCode(500);
            responseResult.setMsg("文件不能为空");
            return  responseResult;
        }

        InputStream inputStream = file.getInputStream();
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        List<Score> scores = ExcelImportUtil.importExcel(inputStream, Score.class, params);

        LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();
        for (Score score : scores) {
            lambda.eq(Score::getExamTime,score.getExamTime())
                    .eq(Score::getStudentNo,score.getStudentNo());
            List<Score> scores1 = scoreMapper.selectList(lambda);
            if (ObjectUtils.isNotEmpty(scores1)){
                responseResult.setMsg(score.getStudentNo()+"的成绩不能重复录入");
                responseResult.setCode(500);
                return  responseResult;
            }
            scoreMapper.insert(score);
        }
        responseResult.success();
        return responseResult;
    }
}




